<?php

namespace Itdashu\Token\Library;

use Itdashu\Token\Bean\Token;
use Swoft\Bean\BeanFactory;

class TokenHelper
{
  /**
   * 获取 token 实例
   * @return Token
   */
  public static function token():Token
  {
    return BeanFactory::getRequestBean(Token::class, \Swoft\Co::tid());
  }

  /**
   * @param $value
   * @return mixed
   */
  public static function decode($value)
  {
    return json_decode((string)$value, true) ?? $value;
  }

  /**
   * @param $value
   * @return false|string
   */
  public static function encode($value)
  {
    if (is_array($value) || is_object($value)) {
      $value = json_encode($value);
    }
    return $value;
  }
}